package Part1_java.util.Collection.Set.Hashset;


import org.junit.Test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;

/**
 * ClassName: SetTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.07 3:26
 */
public class SetTest {
    @Test
    public void test1(){

        HashSet<Object> set = new HashSet<>();
        set.add("AA");
        set.add(123);
        set.add(new User("liu", 12));
        set.add(123);

        Iterator<Object> iterator = set.iterator();
        //可去重
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println(set.contains(new User("liu", 12)));//重写equals()和hashCode():false-->true
    }

    @Test
    public void test2(){

        LinkedHashSet<Object> set = new LinkedHashSet<>();
        set.add("AA");
        set.add(123);
        set.add(new User("liu", 12));
        set.add(123);

        Iterator<Object> iterator = set.iterator();
        //可去重
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println(set.contains(new User("liu", 12)));//重写equals()和hashCode():false-->true
    }
}
